sub get_api_from_flaskfile {
    my $flask_file = shift;
    open(FLASK, "<", $flask_file) or die "can out open $flask_file\n";
    my @lines = <FLASK>;
    close(FLASK);

    my @api;
    for (my $i = 0; $i <= $#lines; $i++) {
        if (not $lines[$i] =~ /CALLPERL/) {
            next;
        }
        else {
            push(@api, $lines[($i + 1)]);
        }
    }

    foreach my $api (@api) {
        $api =~ /.*\((.*)\).*/;
        $api = (split(",", $1, 2))[0];
        # print($api);
    }
    return @api;
}
sub flask_allow_cors {
    my $ filename = shift;
    my @api_paths = @_;
    my $api_path;
    open(FLASKFILE, ">>", $filename) or die "can not open $filename";
    print FLASKFILE "\n\n";
    print FLASKFILE "from flask import make_response\n";
    print FLASKFILE "\n\n";
    my $tab = "    ";
    my $func_body = "${tab}methods = 'GET, POST, OPTIONS'\n${tab}headers = {\n${tab}${tab}'Access-Control-Allow-Origin': '*',\n${tab}${tab}'Access-Control-Allow-Methods': methods,\n${tab}${tab}'Access-Control-Allow-Headers': '*',\n${tab}${tab}'Access-Control-Max-Age': 3600\n${tab}}\n${tab}return make_response('', 200, headers)";
    for (my $i = 0; $i <= $#api_paths; $i++) {
        $api_path = $api_paths[$i];
        $func_name = $api_path;
        $func_name =~ s/\///g;
        $func_name =~ s/'//g;
        $func_name = "perlcors_".$func_name;
        print FLASKFILE "\@app.route( ${api_path}, methods=['OPTIONS'])\ndef ${func_name}():\n$func_body\n";
        print FLASKFILE "\n\n";
    }
    close(FLASKFILE);
}
my $filename = shift;;
my @api = &get_api_from_flaskfile($filename);
&flask_allow_cors($filename, @api);


